package com.situ.springboot.controller;

import com.situ.springboot.pojo.entity.Admin;
import com.situ.springboot.pojo.entity.User;
import com.situ.springboot.pojo.vo.PasswordVo;
import com.situ.springboot.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Objects;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/toAdminInformation")
    public String showAdminInformation(HttpSession session, Model model){
        Admin admin = (Admin) session.getAttribute("admin");
        Integer id = admin.getId();
        Admin admin1 = adminService.selectById(id);
        model.addAttribute("admin",admin1);
        return "/admin/admin_information";
    }

    @RequestMapping("/UpdatePassword")
    public String UpdatePassword(HttpSession session, Model model, PasswordVo passwordVo){
        Admin admin1 = (Admin) session.getAttribute("admin");
        Integer id = admin1.getId();
        Admin admin= adminService.selectById(id);
        if(Objects.equals(passwordVo.getPassword(), "")
                || Objects.equals(passwordVo.getPassword1(), "")
                || Objects.equals(passwordVo.getPassword2(), "")){
            return "index_admin";
        }
        else {
            admin.setPassword(passwordVo.getPassword1());
            adminService.update(admin);
            return "redirect:/user/logout";
        }
    }
}
